<template>
  <el-container style="height: 100vh;">
    <!-- 顶部栏 -->
    <el-header style="background: #fff; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid #eee;">
      <div style="font-family: 'KaiTi', '楷体', serif; font-size: 30px; text-align: left; color:#61AFFE">
        探索科技-个人博客系统
      </div>
      <div>
        <a href="#" @click.prevent="logout">退出登录[{{ username }}]</a>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="220px" style="background: #f5f5f5; border-right: 1px solid #eee;">
          <el-menu
            :default-active="activeMenu"
            class="el-menu-vertical-demo"
            @select="handleMenuSelect"
            router
          >
          <el-menu-item index="/home">
            <span>首页</span>
          </el-menu-item>
          <el-sub-menu index="2">
            <template #title>个人博客管理</template>
            <el-menu-item index="/articles">已发布管理</el-menu-item>
            <el-menu-item index="/draft">草稿管理</el-menu-item>
          </el-sub-menu>
          <el-sub-menu index="3">
            <template #title>系统信息管理</template>
            <el-menu-item index="/department">标签管理</el-menu-item>
            <el-menu-item index="/employee">分类管理</el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <!-- 主内容区 -->
      <el-main style="background: #fff;">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()
const activeMenu = computed(() => route.path)
const username = ref('')

// 获取用户名，触发时机：组件挂载时
onMounted(() => {
  username.value = localStorage.getItem('username')
})

function handleMenuSelect(index) {
  router.push(index)
}

const logout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  router.push('/login')
}
</script>

<style scoped>
/* .body {
  margin: 0;
  padding: 0;
} */
.el-header {
  height: 60px !important;
}
</style>
